<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:48
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
use think\View;

class Base extends Controller
{
    /**
     * 初始化监听登录状态
     */
    public function _initialize()
    {
        if(!session('?user_id')){
            $this->redirect(url('shop/login/indexLogin'));
        }else{
            $userInfo = Db::table('up_u_user')
                ->where('user_id',session('user_id'))
                ->find();
            if($userInfo['user_role']!=2){//不是企业角色
                $this->redirect(url('shop/login/indexLogin'));
            }
            if($userInfo['user_status']!=1){//不是正常状态
                $this->redirect(url('shop/login/indexLogin'));
            }
            $view = new View();
            $view::share("loginUserInfo",$userInfo);
        }
    }

    public function search($search, $page, $fl = 'search')
    {
        if ($search === null && $page === null) {
            session($fl, null);
        } elseif ($search === '') {
            session($fl, null);
        } elseif ($search !== null) {
            session($fl, serialize($search));
        }
        return unserialize(session($fl));
    }
    public function place()
    {
        $area = new \app\common\place\Area;
        $data = $area->area();
        return json($data);
    }
    /**
     * 压缩图片
     * @param $imgsrc:原图片地址（完整的图片路径）
     * @param $imgdst:压缩后的图片地址，也是完整的
     */
    function image_png_size_addshop($imgsrc,$imgdst){
        list($width,$height,$type)=getimagesize($imgsrc);
        switch($type){
            case 1:
                break;
            case 2:
                header('Content-Type:image/jpeg');
                $image = imagecreatefromjpeg($imgsrc);
                imagejpeg($image, $imgdst,50);
                imagedestroy($image);
                break;
            case 3:
                header('Content-Type:image/png');
                $image = imagecreatefrompng($imgsrc);
                imagejpeg($image, $imgdst,50);
                imagedestroy($image);
                break;
        }
    }
    /**
     * 极光推送--推送消息类型-加盟商
     * @param $type 1:推送个人  2:推送全体
     * @param $alias 别名 user_id OR token
     * @param $message 推送消息内容
     * @param $code
     * @return array
     */
    public function Jpush1($type,$alias,$message,$extras,$code)
    {
//        $alias = (string)$alias;
        import('jpush.autoload', EXTEND_PATH);
        //初始化JPushClient
        $client = new \JPush\Client(config('jpush1.appKey'),config('jpush1.masterSecret'));
        //生成推送Payload构建器
        $push = $client->push();
        //推送平台 'all'  OR  ['ios','android']  OR  'ios','android'
        $push->setPlatform('all');
        //1:推送个人  2:推送全体
        if($type==1){
            $push->addAlias($alias);//按别名推送
        }else{
            $push->addAllAudience();//广播消息推送
        }
        //IOS推送
        $push->iosNotification($message, [
                'alert'=>$message,
                'badge' => '+1',
                'extras' => $extras,
                'sound'=>'default'
            ]
        );
        //Android推送
        $push->androidNotification($message, [
                'alert'=>$message,
                'extras' => $extras
            ]
        );
        $extras['key'] = $code;
        $push->message($message, [
            'content_type' => 'text',
            'extras' => $extras
        ]);
        return $push->send();
    }
}